// ---------------------------------------------------------------------
// test.c
// ---------------------------------------------------------------------

#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include "config.h"
#include "simulation.h"
#include "../archivage.h"

static void * ThreadTest();


int main()
{
	pthread_t threadArchivage, threadTest;
	
	// Vers archivage	
	mq_unlink (BAL_versArchivage);
	struct mq_attr lesAttrArch={0,1,sizeof(MSG_versArchivage),0};
	mqd_t queue_handle_versArchivage;
	queue_handle_versArchivage=mq_open(BAL_versArchivage,O_CREAT|O_RDWR,0777, &lesAttrArch);
	if (queue_handle_versArchivage == -1)
	{
		perror ("[TEST] Error Opening MQ queue_handle_versArchivage:");
	}
	
	// Vers Ecriture
	mq_unlink (BAL_versEcriture);
	struct mq_attr lesAttrEcri={0,1,sizeof(MSG_versEcriture),0};
	mqd_t queue_handle_versEcriture;
	queue_handle_versEcriture=mq_open(BAL_versEcriture,O_CREAT|O_RDWR,0777, &lesAttrEcri);
	if (queue_handle_versEcriture == -1)
	{
		perror ("[TEST] Error Opening MQ queue_handle_versEcriture:");
	}
	
	
	
	pthread_create (&threadTest,    NULL, &ThreadTest,    NULL);
	pthread_create (&threadArchivage, NULL, &Archivage, NULL);
	
	
	printf("[MAIN] Attente terminaison threads \n");
	pthread_join (threadArchivage,    NULL);
	pthread_join (threadTest,    NULL);
	printf("[MAIN] Threads terminés \n");
	mq_close(queue_handle_versArchivage);
	mq_unlink(BAL_versArchivage);
	mq_close(queue_handle_versEcriture);
	mq_unlink(BAL_versEcriture);
	return 0;
}

static void * ThreadTest ()
{
	mqd_t queue_handle_versArchivage, queue_handle_versEcriture;
	
	
	queue_handle_versArchivage =  mq_open(BAL_versArchivage,O_WRONLY);
	if (queue_handle_versArchivage == -1)
	{
		perror ("[TEST] Error Opening MQ BAL_versArchivage:");
	}
	
	queue_handle_versEcriture =  mq_open(BAL_versEcriture,O_RDONLY);
	if (queue_handle_versEcriture == -1)
	{
		perror ("[TEST] Error Opening MQ BAL_versEcriture:");
	}

	
	// Lancement des tests
	printf("Debut des tests : \n\n");
	
	int tabSource[4]={T_CONDITIONNEMENT, T_PALETTE, T_ERREUR, T_AU};
	int tabMessage[3]={ERR_IMP, STOCK_OK, DESTOCK_OK};
	int tabType[2]={ARCHIVAGE, ERREUR};
	
	int numTestSource;
	int numTestMessage;
	int numTestType;
	
	MSG_versArchivage messageVersArchivage;
	for(numTestSource=0; numTestSource<=3; numTestSource++)
	{
		messageVersArchivage.source = tabSource[numTestSource];
		
		
		for(numTestMessage=0; numTestMessage<=2; numTestMessage++)
		{
			messageVersArchivage.message = tabMessage[numTestMessage];
			
			for(numTestType=0; numTestType<=1; numTestType++)
			{
				sleep(1);
				printf("\n\nTest numéro %d : Source : %d, Message : %d, Type : %d \n",numTestSource*6 + numTestMessage*2 + numTestType + 1, tabSource[numTestSource] , tabMessage[numTestMessage] , tabType[numTestType]);
				printf("[TEST] Envoie message archivage \n");
				time_t temps = time(NULL);
				messageVersArchivage.heure = temps;
				messageVersArchivage.numLot = 5;
				messageVersArchivage.cartonEnCours = 4;
				messageVersArchivage.type = tabType[numTestType];
				
				if (0 != mq_send (queue_handle_versArchivage, (const char*) &messageVersArchivage, sizeof(MSG_versArchivage), 2))
				{
					perror ("[TEST] Sending to BAL_versArchivage:");
				}
				
				
				if (messageOperateur(messageVersArchivage.message) == 1)
				{
					// Attente du dépôt d'un message dialogue opérateur
					printf("[TEST] Attente de l'affichage opérateur \n");
					MSG_versEcriture messageEcriture;
					int bytes_read = mq_receive(queue_handle_versEcriture, (char*) &messageEcriture, sizeof(MSG_versEcriture), NULL);

					if (bytes_read == -1)
					{
						perror("[TEST] Failed to recieve BAL_versEcriture:");
					}
					printf("[TEST] messageEcriture : type %d, Message %d, numLot %d, cartonEnCours %d \n", messageEcriture.typeMessage, messageEcriture.message, messageEcriture.numLot, messageEcriture.cartonEnCours); 
				}
			}
		}
	}
	printf("\n\nTest numéro 25 : Fin  production\n");
	printf("[TEST] Envoie message archivage \n");
	time_t temps = time(NULL);
	messageVersArchivage.source = T_STOCKAGE;
	messageVersArchivage.message = FIN_PRODUCTION;
	messageVersArchivage.heure = temps;
	messageVersArchivage.numLot = 5;
	messageVersArchivage.cartonEnCours = 4;
	messageVersArchivage.type = ARCHIVAGE;
	
	if (0 != mq_send (queue_handle_versArchivage, (const char*) &messageVersArchivage, sizeof(MSG_versArchivage), 2))
	{
		perror ("[TEST] Sending to BAL_versArchivage:");
	}
	
	
	if (messageOperateur(messageVersArchivage.message) == 1)
	{
		// Attente du dépôt d'un message dialogue opérateur
		printf("[TEST] Attente de l'affichage opérateur \n");
		MSG_versEcriture messageEcriture;
		int bytes_read = mq_receive(queue_handle_versEcriture, (char*) &messageEcriture, sizeof(MSG_versEcriture), NULL);

		if (bytes_read == -1)
		{
			perror("[TEST] Failed to recieve BAL_versEcriture:");
		}
		printf("[TEST] messageEcriture : type %d, Message %d, numLot %d, cartonEnCours %d \n", messageEcriture.typeMessage, messageEcriture.message, messageEcriture.numLot, messageEcriture.cartonEnCours); 
	}
	// Fermeture des BàL
	mq_close (queue_handle_versArchivage);
	mq_close (queue_handle_versEcriture);
}

